<?php

namespace App\Http\Controllers\Api;

use App\Repositories\Admin\UserRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class LoginController extends Controller
{
    protected $userRepository;
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    //获取openID
    public function getOpenId(Request $request){
        $getOpenIdApi = $request->post('getOpenIdApi');
        $appid = $request->post('appid');
        $secret = $request->post('secret');
        $js_code = $request->post('js_code');
        $grant_type = $request->post('grant_type');

        $url = $getOpenIdApi.'?appid='.$appid.'&secret='.$secret.'&js_code='.$js_code.'&grant_type='.$grant_type;
        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($con, CURLOPT_TIMEOUT, 5);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($con);
        curl_close($con);
        return $result;
    }
//  使用openid登录
    public function login(Request $request){
	$userOpenId = $request->post('user_open_id');
        $user = $this->userRepository->getByOpenId($userOpenId);
        if($user){
            return json_encode(['code'=>200,'user'=>$user]);
        }else{
            return json_encode(['code'=>400,'msg'=>'此用户还未注册','openId'=>$userOpenId]);
        }
    }
    public function register(Request $request){
	    $userData = $request->post('userInfo');
        $userOpenId = $request->post('user_open_id');
        $user = $this->userRepository->getByOpenId($userOpenId);
        #return json_encode($user);
        if($userOpenId && !$user){
                $user = [];
                if ($userData['avatarUrl'])
                    $user['user_thumbnail'] = $userData['avatarUrl'];
                if ($userData['nickName'])
                    $user['user_nickname'] = $userData['nickName'];
                if ($userData['gender'])
                    $user['user_sex'] = $userData['gender'];
                if ($userData['province'])
                    $user['user_province'] = $userData['province'];
                if ($userData['city'])
                    $user['user_city'] = $userData['city'];

                $user['user_open_id'] = $userOpenId;

                $user['user_money'] = 0;
                $user['user_rank_number'] = 0;
                $user['user_all_ticket'] = Redis::get('userTicket');
                $user['user_ticket'] = Redis::get('userTicket');
                $user['user_uprank_ticket'] = $this->userRepository->getRankCondition(1,1);
                $user['user_used_ticket'] = 0;
                $user['user_status'] = 1;
                $user['user_allow_chat'] = 0;
                $user['user_type'] = 2;

                $res = array();
                $res = $this->userRepository->createUser($user);
                if($res){
                    //男/女数增加
                    if($user['user_sex'] == 1){
                        if(Redis::exists('maleNumber')){
                            $maleNumber = Redis::get('maleNumber');
                            $maleNumber++;
                            Redis::set('maleNumber',$maleNumber);
                        }else{
                            Redis::set('maleNumber',1);
                        }
                    }else if($user['user_sex'] == 2) {
                        if(Redis::exists('femaleNumber')){
                            $femaleNumber = Redis::get('femaleNumber');
                            $femaleNumber++;
                            Redis::set('femaleNumber',$femaleNumber);
                        }else{
                            Redis::set('femaleNumber',1);
                        }
                    }
                    //用户总数增加
                    if(Redis::exists('userNumber')){
                        $userNumber = Redis::get('userNumber');
                        $userNumber++;
                        Redis::set('userNumber',$userNumber);
                    }else{
                        Redis::set('userNumber',1);
                    }
                    //今日新增用户增加
                    if(Redis::exists('dayUserNumber')){
                        $dayUserNumber = Redis::get('dayUserNumber');
                        $dayUserNumber++;
                        Redis::set('dayUserNumber',$dayUserNumber);
                    }else{
                        Redis::set('dayUserNumber',1);
                    }
            $user = $this->userRepository->getByOpenId($userOpenId);
                    return json_encode(['code'=>200,'user'=>$user]);
                }else{
            return json_encode(['code'=>400,'msg'=>'保存失败']);
                }
            }else{
             $user = $this->userRepository->getByOpenId($userOpenId);
    #                return json_encode($user);
                    if($user){
                     return json_encode(['code'=>200,'user'=>$user]);
                    }else{
                            return json_encode(['code'=>400,'msg'=>'此用户还未注册']);
                      }

        }
    }
    /**
     * 更新用户信息
     */
    public function update(Request $request){
        $userData = $request->post('userInfo');
        $userId = $request->post('user_id');
        if(!empty($userData) && $userId){
            $user = [];
            if ($userData['avatarUrl'])
                $user['user_thumbnail'] = $userData['avatarUrl'];
            if ($userData['nickName'])
                $user['user_nickname'] = $userData['nickName'];
            if ($userData['gender'])
                $user['user_sex'] = $userData['gender'];
            if ($userData['province'])
                $user['user_province'] = $userData['province'];
            if ($userData['city'])
                $user['user_city'] = $userData['city'];
            $result = $this->userRepository->updateUser($userId,$user);
            if($result){
                return json_encode(['code'=>200,'user'=>$result]);
            }else{
                return json_encode(['code'=>400,'msg'=>'修改失败']);
            }
        }
    }

}
